﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.helper;
using WindowsFormsApp1.model;

namespace WindowsFormsApp1
{
    public partial class JobListsForm : Form
    {
        int selectIndex;
        public JobListsForm()
        {
            InitializeComponent();

            //lVJobLists.BeginUpdate();
            loadJobListHeader();
            loadJobLists();
            //lVJobLists.EndUpdate();
            selectIndex = -1;
        }
        private void loadJobListHeader()
        {
            List<JobListHeaderModel> lths = DataHelper.GetInstance().JobListHeader;
            foreach (var item in lths)
            {
                ColumnHeader ch = new ColumnHeader();
                ch.Text = item.HeaderName;   //设置列标题
                ch.Width = item.Width;    //设置列宽度
                ch.TextAlign = (HorizontalAlignment)item.TextAlign;
                lVJobLists.Columns.Add(ch);
            }
            loadJobLists();
        }
        private void loadJobLists()
        {
            lVJobLists.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            List<ShiGongTianChuangModel> lts = DataHelper.GetInstance().JobList;
            
            lVJobLists.Items.Clear();

            ////将lt数据添加到listView1控件中
            foreach (var item in lts)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = item.JobName;
                lvi.SubItems.Add(item.JobIndex);
                lvi.SubItems.Add(item.JobAddress);
                lvi.SubItems.Add(item.JobNature);
                lvi.SubItems.Add(item.JobSubjectCompany);
                string obj = null;
                foreach (var st in item.JobObjectCompany)
                {
                    obj += st + ",";
                }
                lvi.SubItems.Add(obj.TrimEnd(','));
                lvi.SubItems.Add(item.JobStatTime.ToShortTimeString());
                lvi.SubItems.Add(item.JobTotalTime.ToString());

                this.lVJobLists.Items.Add(lvi);
            }
           
        }

        private void lVJobLists_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            foreach (ListViewItem lv in lVJobLists.CheckedItems)
            {
                lv.Checked = false;

            }
            e.Item.Checked = true;
        }

        private void lVJobLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lv in lVJobLists.Items)
            {

                if (lv.Selected)
                {
                    //if (lv.Checked)  
                    //{  
                    //    //lv.Checked = false;  
                    //}  
                    //else  
                    //{  
                    lv.Checked = true;
                    selectIndex = lv.Index;
                    //}  
                }
                else
                {
                    if (lVJobLists.SelectedIndices.Count > 0)
                    {
                        if (lv.Checked)
                        {
                            lv.Checked = false;
                        }
                    }

                }
            }
        }

        private void lVJobLists_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (!lVJobLists.Items[e.Index].Checked)//如果点击的CheckBoxes没有选中  
            {
                foreach (ListViewItem lv in lVJobLists.Items)
                {
                    if (lv.Checked)//取消所有已选中的CheckBoxes  
                    {
                        lv.Checked = false;
                        lv.Selected = false;
                        // lv.BackColor = Color.White;  
                    }
                }
                lVJobLists.Items[e.Index].Selected = true;
                selectIndex = e.Index;
                // lv.Checked = false;  
            }

            //int count = listView1.Items.Count;  
            //ListViewItem item = listView1.Items[e.Index];  
            //if (item.Checked)  
            //{  
            //    for (int i = 0; i < count; i++)  
            //    {  
            //        if (i != e.Index)  
            //        {  
            //            ListViewItem item1 = listView1.Items[i];  
            //            item1.Checked = false;  
            //        }  
            //    }  
            //}  
        }

        private void btnStatJob_Click(object sender, EventArgs e)
        {
            if (selectIndex == -1)
            {
                MessageBox.Show("没有选中作业");
                return;
            }
            if (cbBTotalTime.Text.Trim() == "" || dTPStatTime.Text.Trim()=="")
            {
                MessageBox.Show("开始时间或者总时长没写");
                return;
            }
            List<ShiGongTianChuangModel> lts = DataHelper.GetInstance().JobList;
            lts[selectIndex].JobStatTime = dTPStatTime.Value;
            lts[selectIndex].JobTotalTime = int.Parse(cbBTotalTime.Text);
            DataHelper.GetInstance().refrashJobList();
            loadJobLists();
        }
    }
}
